<?php

namespace app\api\controller;

use app\api\service\AddressBook as AddressBookService;
use app\common\controller\Api;
use think\exception\ValidateException;

/**
 * 示例接口
 */
class AddressBook extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    // protected $noNeedLogin = [''];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = '*';
    private $addressBookService;

    public function __construct()
    {
        parent::__construct();
        $this->addressBookService = new AddressBookService;
    }

    /**
     * 查询当前登录用户的所有地址信息
     *
     */
    public function list()
    {
        $userId = $this->auth->__get('id');
        $list = $this->addressBookService->showAddressBook($userId);
        return $this->success('返回成功', $list);
    }

    /**
     * 查询默认地址
     *
     */
    public function default()
    {
        $userId = $this->auth->__get('id');
        $ret = $this->addressBookService->showDefaultAddressBook($userId);
        return $this->success('返回成功', $ret);
    }

    /**
     * 新增地址
     *
     */
    public function add()
    {
        $data = $this->request->post();
        // 参数校验
        $validate  = validate('AddressBook');
        if (!$validate->scene('add')->check($data)) {
            throw new ValidateException($validate->getError());
        }
        if (empty($data['userId'])) {
            $data['userId'] = $this->auth->__get('id');
        }
        $this->addressBookService->addAddressBook($data);
        return $this->success();
    }

    /**
     * 根据id修改地址
     *
     */
    public function update()
    {
        $data = $this->request->put();
        // 参数校验
        $validate  = validate('AddressBook');
        if (!$validate->scene('update')->check($data)) {
            throw new ValidateException($validate->getError());
        }
        if (empty($data['userId'])) {
            $data['userId'] = $this->auth->__get('id');
        }
        $this->addressBookService->updateAddressBook($data);
        return $this->success();
    }

    /**
     * 根据id删除地址
     *
     */
    public function delete()
    {
        $data = $this->request->delete();
        // 参数校验
        $validate  = validate('AddressBook');
        if (!$validate->scene('delete')->check($data)) {
            throw new ValidateException($validate->getError());
        }
        $id = $data['id'];
        $this->addressBookService->deleteAddressBook($id);
        return $this->success();
    }

    /**
     * 设置默认地址
     *
     */
    public function setDefault()
    {
        $data = $this->request->put();
        // 参数校验
        $validate  = validate('AddressBook');
        if (!$validate->scene('setDefault')->check($data)) {
            throw new ValidateException($validate->getError());
        }
        $id = $data['id'];
        $this->addressBookService->setDefaultAddressBook($id);
        return $this->success();
    }

    /**
     * 根据id查询地址
     *
     * @param int $id
     */
    public function read($id){
        $userId = $this->auth->__get('id');
        $ret = $this->addressBookService->getAddressBookById($id,$userId);
        return $this->success('返回成功',$ret);
    }
}
